import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

public class OutServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        PrintWriter out=response.getWriter();
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        out.print("<!DOCTYPE html>");
        out.print("<html lang='en'>");
        out.print("<head>");
        out.print("<meta charset='UTF-8'>");
        out.print("<title>建材库存表</title>");
        out.print("</head>");
        out.print("<body>");
        out.print("<h2 align='center'>建材库存表</h2>");
        out.print("<hr>");
        out.print("<table align='center' border='1px' width='30%'>");
        out.print("<tr align='center'>");
        out.print("<td>id</td>");
        out.print("<td>name</td>");
        out.print("<td>amount</td>");
        out.print("</tr>");
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/jcdb", "root", "19970913Xun");
            String sql = "select id,name,amount from t_jcamount";
            ps = conn.prepareStatement(sql);
            rs = ps.executeQuery();
            //用于自增的id
            while (rs.next()) {
                String id = rs.getString("id");
                String name = rs.getString("name");
                String amount = rs.getString("amount");
                out.print(" <tr align='center'>");
                out.print("<td>" + (id) + "</td>");
                out.print("<td>" + (name) + "</td>");
                out.print("<td>" + (amount) + "</td>");
                out.print("</tr>");
            }
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            if (rs != null) {
                try {
                    rs.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (ps != null) {
                try {
                    ps.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
            if (conn != null) {
                try {
                    conn.close();
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
        }
        out.print("</table>");
        out.print("</body>");
        out.print("</html>");
        out.print("<p align=\"center\"><a href=\"/JCManagementSystem/OutManage.html\"> <input type=\"button\" value=\"出库\"></a></p>\n" );
        out.print("<p align=\"center\"><a href=\"/JCManagementSystem/Main.html\"> <input type=\"button\" value=\"返回\"></a></p>\n" );
    }
}
